<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">
                首页</el-breadcrumb-item
            >
            <el-breadcrumb-item :to="{ path: '/page' }">
                权限管理</el-breadcrumb-item
            >
            <el-breadcrumb-item :to="{ path: '/page' }"
                >页面管理</el-breadcrumb-item
            >
            <el-breadcrumb-item>编辑页面</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card class="box-card">
            <el-page-header @back="goBack" content="编辑页面"> </el-page-header>
            <el-form
                :model="editForm"
                :rules="addFormRules"
                ref="addFormRef"
                label-width="180px"
                hide-required-asterisk
            >
                <el-form-item label="父级页面" prop="parent_id" align="left">
                    <el-tooltip
                        :enterable="false"
                        effect="dark"
                        content="不选择则页面为根页面"
                        placement="top"
                    >
                        <select-tree
                            ref="setTree"
                            @subSelectData="setParentId"
                        ></select-tree>
                    </el-tooltip>
                </el-form-item>
                <el-form-item label="页面图标" prop="icon">
                    <el-input v-model="editForm.icon"> </el-input>
                </el-form-item>
                <el-form-item label="页面标题（中文）" prop="title">
                    <el-input v-model="editForm.title"> </el-input>
                </el-form-item>
                <el-form-item label="页面名称标识（英文）" prop="name">
                    <el-input v-model="editForm.name"> </el-input>
                </el-form-item>
                <el-form-item label="组件文件路径" prop="component_path">
                    <el-tooltip
                        :enterable="false"
                        effect="dark"
                        content="参考'page/Add.vue'"
                        placement="top"
                    >
                        <el-input v-model="editForm.component_path"> </el-input>
                    </el-tooltip>
                </el-form-item>
                <el-form-item label="菜单类型" align="left">
                    <el-switch
                        v-model="editForm.type"
                        :active-value="1"
                        :inactive-value="2"
                        inline-prompt
                        active-color="#13ce66"
                        inactive-color="#e6a23c"
                        active-text="menu"
                        inactive-text="act"
                    ></el-switch>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input v-model="editForm.sort"> </el-input>
                </el-form-item>
                <el-form-item label="状态" align="left">
                    <el-switch
                        v-model="editForm.status"
                        :active-value="1"
                        :inactive-value="0"
                    ></el-switch>
                </el-form-item>
                <el-form-item align="left">
                    <el-button type="primary" @click="subEdit">保存</el-button>
                    <el-button @click="resetForm">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script>
import requestApis from '@/request'
import SelectTree from '@/components/select_tree/SelectTree.vue'
export default {
    components: { SelectTree },
    data () {
        return {
            editForm: {
                id: 0,
                parent_id: 0,
                icon: '',
                name: '',
                title: '',
                router_path: '',
                component_path: '',
                sort: 10,
                type: 1,
                status: 1
            },
            // 添加表单验证规则对象
            addFormRules: {
                icon: [
                    { min: 6, max: 34, message: '图标内容长度在6~34位之间', trigger: 'blur' }
                ],
                title: [
                    { required: true, message: '请输入页面标题', trigger: 'blur' },
                    { min: 2, max: 16, message: '页面标题长度在2~6位之间', trigger: 'blur' }
                ],
                name: [
                    { required: true, message: '请输入页面名称', trigger: 'blur' },
                    { min: 2, max: 16, message: '页面名称长度在2~6位之间', trigger: 'blur' }
                ],
                component_path: [
                    { required: true, message: '请输入组件文件路径', trigger: 'blur' },
                    { min: 2, max: 26, message: '组件文件路径长度在2~6位之间', trigger: 'blur' }
                ],
                sort: [
                    { required: true, message: '请输入排序', trigger: 'blur' }
                ]
            }
        }
    },
    mounted () {
        this.getTreeMenuAll()
        this.getMenuRow(this.$route.query.id)
    },
    methods: {
        getMenuRow (id) {
            requestApis.getMenuRow({ id }).then(res => {
                if (res.meta.code === 1) {
                    this.editForm.id = res.data.id
                    this.editForm.parent_id = res.data.parent_id
                    this.editForm.icon = res.data.icon
                    this.editForm.name = res.data.name
                    this.editForm.title = res.data.title
                    this.editForm.component_path = res.data.component_path
                    this.editForm.sort = res.data.sort
                    this.editForm.type = res.data.type
                    this.editForm.status = res.data.status
                    this.$refs.setTree.subValue(res.data.parent_id)
                } else {
                    this.$message({
                        message: res.meta.msg,
                        type: 'warning'
                    })
                    this.$router.push('/admin')
                }
            }).catch(error => {
                console.log(error)
            })
        },
        goBack () {
            this.$router.back()
        },
        setParentId (parentId) {
            this.editForm.parent_id = parentId
        },
        getTreeMenuAll () {
            requestApis.getTreeMenuAll().then(res => {
                if (res.meta.code === 1) {
                    this.$refs.setTree.setSelectData(res.data)
                } else {
                    this.mg({
                        message: res.meta.msg,
                        type: 'warning'
                    })
                }
            }).catch(error => {
                console.log(error)
            })
        },
        subEdit () {
            this.$refs.addFormRef.validate(valid => {
                if (!valid) {
                    return false
                }
                requestApis.saveMenuEdit(this.editForm).then(res => {
                    if (res.meta.code === 1) {
                        this.$message({
                            message: res.meta.msg,
                            type: 'success'
                        })
                        this.$router.back()
                    } else {
                        this.$message({
                            message: res.meta.msg,
                            type: 'warning'
                        })
                    }
                }).catch(error => {
                    console.log(error)
                })
            })
        },
        resetForm () {
            this.$refs.addFormRef.resetFields()
            this.addForm.status = 1
        }
    }
}
</script>

<style>
.el-form {
    width: 60%;
    margin-top: 20px;
}
</style>
